package com.example.trader.controller;


import com.example.trader.common.Result;
import com.example.trader.entity.Product;
import com.example.trader.service.ProductManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/trader")
@CrossOrigin(origins = "http://localhost:8080")
public class ProductManagerController {

    @Autowired
    private ProductManagerService productManagerService;

    /**
     * 商品信息列表展示接口
     */
    @GetMapping("/getProductManagerPage")
    public Result<PageInfo> getProductManagerPage(@RequestParam(defaultValue = "1") Integer pageNumber,@RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNumber, pageSize);
        List<Product> productList=productManagerService.getProductList();
        return Result.success(new PageInfo(productList));
    }
    /**
     * 新增商品信息
     */

    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product){
        return productManagerService.addProduct(product);
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteProduct/{id}")
    public Result deleteProduct(@PathVariable Integer id){
        return productManagerService.deleteProduct(id);
    }

    /**
     * 根据id进行编辑信息
     */

    @PutMapping("/editProductInfo")
    public Result editProductInfo(@RequestBody Product product){
        return productManagerService.editProductInfo(product);
    }

    /**
     * 根据商品名称进行查询
     */
    @GetMapping("/getProductPageByName")
    public  Result<PageInfo> getProductPageByName(@RequestParam("productName") String productName)
    {
        PageHelper.startPage(1,10);
       List<Product> productList= productManagerService.getProductByName(productName);
       return Result.success(new PageInfo(productList));
    }


}
